package com.wc.alorithm_blue_bridge._思维.宝塔;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/6/3 16:18
 * @description
 * https://www.lanqiao.cn/problems/20111/learning/?problem_list_id=33&page=1
 */
public class Main {
    /**
     * 顶级思路：
     * 找到全排列左右可能的值
     * 然后开始套用dfs就得了
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 10, M = 6, L = 3500, P = 6;
    static int[]
            up = {0, 2, 2, 1, 3, 3},
            down = {0, 1, 4, 2, 2, 3},
            left = {0, 2, 2, 3, 2, 1},
            right = {0, 3, 3, 1 ,2 ,4};
    static int[][] g = new int[P][P];
    static int[][][][] f = new int[P][P][L][P];
    static boolean[] use = new boolean[N];
    static int[] num = new int[N];

    public static void main(String[] args) {
        dfs(1);
        dfs2(1);
        for (int i = 1; i < P; i++) {
            for (int j = 1; j < P; j++) out.print(g[i][j]);
        }
        out.flush();
    }

    static boolean dfs2(int u) {
        if (u == P) return check();

        int x = left[u], y = right[u], cnt = f[x][y][0][0];
        for (int i = 1; i <= cnt; i++) {
            for (int j = 1; j < P; j++) g[u][j] = f[x][y][i][j];
            if (dfs2(u + 1)) return true;
        }

        return false;
    }

    static boolean check () {
        // 查每列是否为 1 ~ 5
        for (int i = 1; i < P; i++) {
            int sum = 0;
            for (int j = 1; j < P; j++) sum += g[j][i];
            if (sum != 15) return false;
        }

        // 是否满足上下列
        for (int i = 1; i < P; i++) {
            int x = up[i], y = down[i], cnt = f[x][y][0][0];
            boolean success = false;
            for (int j = 1; j <= cnt; j++) {
                boolean exist = true;
                for (int k = 1; k <= 5; k++) {
                    if (g[k][i] != f[x][y][j][k]) {
                        exist = false;
                        break;
                    }
                }
                if (exist) {
                    success = true;
                    break;
                }
            }
            if (!success) return false;
        }

        return true;
    }

    static void dfs(int u) {
        if (u == P) {
            int x = work();
            reverse();
            int y = work();
            reverse();
            int cnt = ++f[x][y][0][0];
            for (int i = 1; i < P; i++) {
                f[x][y][cnt][i] = num[i];
            }
            return;
        }
        for (int i = 1; i <= 5; i++) {
            if (use[i]) continue;
            use[i] = true;
            num[u] = i;
            dfs(u + 1);
            use[i] = false;
        }
    }

    static void reverse() {
        for (int i = 1, j = 5; i < j; i++, j--) {
            int tmp = num[i];
            num[i] = num[j];
            num[j] = tmp;
        }
    }

    static int work () {
        int x = num[1];
        int res = 1;
        for (int i = 2; i <= 5; i++) {
            if (num[i] > x) {
                x = num[i];
                res ++;
            }
        }
        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}



